const express = require('express');
const router = express.Router();
const pool = require('../db');

// 获取今日访客和历史访客数量
router.get('/visitor-stats', async (req, res) => {
    try {
        const today = new Date().toISOString().split('T')[0];

        // 获取今日访客数量
        const [todayResult] = await pool.query(
            'SELECT visitor_count FROM daily_visitor_count WHERE date = ?',
            [today]
        );

        // 获取历史总访客数量
        const [totalResult] = await pool.query(
            'SELECT SUM(visitor_count) as total_visitors FROM daily_visitor_count'
        );

        res.json({
            today_visitors: todayResult[0]?.visitor_count || 0,
            total_visitors: totalResult[0].total_visitors || 0
        });
    } catch (error) {
        console.error('获取访客统计失败:', error);
        res.status(500).json({ error: '获取访客统计失败' });
    }
});

module.exports = router; 